﻿namespace Bizspoke.BizTalk.Brma
{
    using Microsoft.BizTalk.Tracing;
    using Microsoft.BizTalk.Tracking;
    using System;
    using System.Drawing;
    using System.IO;
    using System.Windows.Forms;

    internal class DirBrowser
    {
        private string _description = "Choose Directory";
        private string _returnPath = string.Empty;
        private FolderBrowserDialog fb = new FolderBrowserDialog();
        private ResourcesNav resNav = new ResourcesNav();

        public string RequestFolderLocationWithChecks()
        {
            this.WarnAboutSecurityImplicationsInSaveMessages();
            DirBrowser browser = new DirBrowser();
            browser.Description = this.resNav.GetString("adm_ChooseSaveToFolder");
            bool flag = false;
            DialogResult result = browser.ShowDialog();
            if (result == DialogResult.OK)
            {
                while (!flag)
                {
                    if (browser.ReturnPath.Length == 0)
                    {
                        MessageBox.Show(this.resNav.GetString("adm_ChooseAnotherFolder"), this.resNav.GetString("adm_ChooseSaveToFolder"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    else
                    {
                        DirectoryInfo info = new DirectoryInfo(browser.ReturnPath);
                        if ((info.Attributes & FileAttributes.Directory) != FileAttributes.Directory)
                        {
                            MessageBox.Show(this.resNav.GetString("adm_ChooseAnotherFolder"), this.resNav.GetString("adm_ChooseSaveToFolder"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        }
                        else
                        {
                            string path = browser.ReturnPath + @"\0zdr_fw9.tmp";
                            try
                            {
                                FileStream stream = File.Create(path);
                                if (stream != null)
                                {
                                    stream.Close();
                                    File.Delete(path);
                                    flag = true;
                                }
                            }
                            catch (Exception exception)
                            {
                                Trace.Tracer.TraceMessage(4, "BTSHATAPP: Exception {0} at {1}", new object[] { exception.Message, exception.StackTrace });
                                MessageBox.Show(this.resNav.GetString("adm_ReadOnlyFolder"), this.resNav.GetString("adm_ChooseSaveToFolder"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
                            }
                        }
                    }
                    if (!flag)
                    {
                        result = browser.ShowDialog();
                    }
                    if (result == DialogResult.Cancel)
                    {
                        break;
                    }
                }
            }
            if (result == DialogResult.OK)
            {
                return browser.ReturnPath;
            }
            return null;
        }

        public DialogResult ShowDialog()
        {
            this.fb.Description = this._description;
            this.fb.SelectedPath = Preferences.LastSaveMsgBodyPath;
            DialogResult result = this.fb.ShowDialog();
            Preferences.LastSaveMsgBodyPath = this.fb.SelectedPath;
            if (result == DialogResult.OK)
            {
                this._returnPath = this.fb.SelectedPath;
                return result;
            }
            this._returnPath = string.Empty;
            return result;
        }

        private DialogResult WarnAboutSecurityImplicationsInSaveMessages()
        {
            string msg = SharedRes.NavCtrl.GetString("wrn_SecurityImplicationsForSaveMessagesToLocalHD");
            string caption = SharedRes.NavCtrl.GetString("warn_Caption");
            ErrorDlg dlg = new ErrorDlg(msg, caption, "SecurityImplicationsForSaveMessagesToLocalHDWarning", SystemIcons.Warning, MessageBoxButtons.OK, MessageBoxDefaultButton.Button1);
            return dlg.ShowDialog();
        }

        public string Description
        {
            get
            {
                return this._description;
            }
            set
            {
                this._description = value;
            }
        }

        public string ReturnPath
        {
            get
            {
                return this._returnPath;
            }
        }
    }
}

